﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Public Class DeleteAccountPageViewModel : Inherits ObservableObject

    Private _userId As String
    Public Property UserId As String
        Get
            Return _userId
        End Get
        Set(value As String)
            SetProperty(_userId, value)
        End Set
    End Property

    Private _password As String
    Public Property Password As String
        Get
            Return _password
        End Get
        Set(value As String)
            SetProperty(_password, value)
        End Set
    End Property

    Private _errorMessage As String
    Public Property ErrorMessage As String
        Get
            Return _errorMessage
        End Get
        Set(value As String)
            SetProperty(_errorMessage, value)
        End Set
    End Property

    Private ReadOnly _client As HttpClientBase

    Public Property ConfirmDeleteAccountCommand As New AsyncRelayCommand(AddressOf ConfirmDeleteAccountClicked)
    Public Property CancelCommand As New RelayCommand(AddressOf CancelClicked)

    Public Sub New()
        _client = New HttpClientBase()
    End Sub

    Private Async Function ConfirmDeleteAccountClicked() As Task
        Try
            If UserId = CType(Application.Current.Resources("UserId"), String) Then
                '从服务器读取密码
                Dim response = Await _client.GetAsync(Of User)("vdm/api/account/findUser/id/" & UserId)
                Dim one = response.Data
                If one IsNot Nothing AndAlso one.Password = Password Then
                    '删除用户
                    If MessageBox.Show("最后再确认一次，您真的要删除账户吗？", MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
                        '从服务器删除用户信息
                        response = Await _client.DeleteAsync(Of User)("vdm/api/account/deleteAccount/" & UserId)
                        If Not response.Code = 200 Then
                            ErrorMessage = $"操作失败：{response.Message}"
                            Return
                        End If
                        Dim GoodByeLetter As String = "再见，" & one.Username & "！" & vbCrLf _
                        & "愿我们以后都能共同进步，都能有更美好的未来。" & vbCrLf _
                        & "感谢您的使用！" & vbCrLf _
                        & "我们会一直等你回来！" & vbCrLf _
                        & "承载漂流瓶的汪洋中永远会有你的一席之地。"
                        MessageBox.Show(GoodByeLetter)
                        Application.Current.Resources.Remove("UserId")
                        NavigationService.Navigate(Of LoginPage)()
                    End If
                Else
                    ErrorMessage = "密码错误，请重新输入！"
                End If
            Else
                ErrorMessage = "用户 ID 错误，请重新输入！"
            End If
        Catch ex As Exception
            MessageBox.Show($"系统错误：{ex.Message}")
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
        End Try

    End Function

    Private Sub CancelClicked()
        NavigationService.Navigate(NavigationData.MAIN_LAYOUT_URI)
    End Sub

End Class
